Skip to content

frontend: /metrics with request gauges and token counters - #12

Open
100menotu001 wants to merge 5 commits into
Picom-code:mainfrom
100menotu001:frontend-metrics
Open

100menotu001 wants to merge 5 commits into
Picom-code:mainfrom
100menotu001:frontend-metrics

Conversation

@100menotu001

@100menotu001 100menotu001 commented Nov 12, 2026 •

Copy link
Copy Markdown

Adds GET /metrics to dsfront (Prometheus text, open like /health). Today the front end exposes no load or token counters, so external dashboards and schedulers can't tell how busy the engine is.

Exported:

  • requests_running, requests_waiting — from the admission gate
  • generation_tokens_total, prompt_tokens_total, prompt_tokens_cached_total — monotonic
  • time_to_first_token_seconds_sum / _count

I used the tensorfold: prefix so tools that already scrape that engine's series (dashboards, schedulers) read this one unchanged. Happy to rename to a dsgb10_ prefix if you'd rather keep the namespace separate; it's a one-line change per series.

Notes:

  • Counters are process-wide statics updated at request completion (tokens as they stream), so they reset when the front end restarts.
  • No new dependencies. Built with cargo build --release, and running on a 4-node TP4 setup (16 slots) against live traffic.
  • No histograms, slot gauge or ITL yet.

GET /metrics (unauthenticated like /health) exports, in Prometheus text format:
requests_running / requests_waiting from the admission gate, and monotonic
generation, prompt and cached-prompt token totals plus a time-to-first-token
sum/count. The names use the tensorfold: prefix so existing dashboards and
schedulers that read that engine's series work unchanged.
@robertung88

Copy link
Copy Markdown

Useful PR — we're in exactly the position it describes.

We run dsfront on four GB10 Sparks (switchless ConnectX-7 ring, TP=4, DeepSeek-V4.1-Flash-1017, f211791) as the main serving lane, and we scrape it from sparkDash, an open-source dashboard for DGX Spark fleets.

Two points from the consuming side:

1. Please keep the tensorfold: prefix. That is what makes this a no-op for existing scrapers. sparkDash already reads tensorfold:prompt_tokens_total, tensorfold:prompt_tokens_cached_total (plus the prompt_cache_cached_tokens / prefix_cache_hits_total fallbacks) and the generation counters, and derives prefill/decode rates and a prefix-cache hit rate from consecutive scrapes. dsgb10_ would work too, but it means a second reader on our side for series that are semantically identical.

2. One semantic detail on the counters, because it decides whether the derived rates are right. sparkDash computes computed-prefill as prompt_tokens_total − prompt_tokens_cached_total and clamps the cached delta to the same scrape's prompt delta. So prompt_tokens_cached_total needs to be a subset of prompt_tokens_total (tokens served from prefix cache, counted inside the prompt total), not an additional counter. If dsfront counts them the other way round, the prefill rate comes out inflated by the cache share — on a cache-heavy agent lane that is the difference between ~1.1k and ~26k tok/s.

A slot/queue gauge beyond requests_running/requests_waiting is not needed for us; those two plus the token counters already give everything we display.

Right now the dashboard classifies owned_by: dsgb10 as a distinct pico backend and deliberately does not scrape /metrics at all (it sends one synthetic ~120-token completion every 15 s instead, purely to have some decode number, and the token ledger records nothing for the unit). Once this lands we would rather read the engine's own counters and drop the synthetic probe. Happy to test the branch against our ring if that is useful.

…otal

sparkDash derives the prefill rate as prompt_tokens_total minus
prompt_tokens_cached_total, and clamps the cached delta to the same scrape's
prompt delta. That only comes out right if the cached counter counts reused
prompt tokens that are already inside the prompt total, rather than an extra
series alongside it.

That is what the engine reports (Done::cached_tokens is the reused prefix
length, and the front end already adds the full prompt length to the prompt
total), but nothing in the code said so and nothing stopped an engine from
reporting a longer reuse than the prompt it was handed. Document the
invariant where the counters live and clamp the cached value at the update
site, so a bad reuse length can no longer make the derived prefill rate go
negative or inflate it by the cache share.
@100menotu001

Copy link
Copy Markdown
Author

Thanks — both points are now explicit rather than incidental.

1. The tensorfold: prefix stays. No rename; that was the reason it was written that way.

2. prompt_tokens_cached_total is a subset of prompt_tokens_total, not an additional counter. The front end adds the full prompt length (prompt_ids.len()) to the prompt total and only the engine's Done.cached_tokens to the cached counter, and the protocol defines that field as "prompt tokens whose KV was reused, not computed" (docs/protocol.md) — the reused prefix of the same prompt, so prompt − cached is the tokens actually prefilled. Your computed-prefill derivation is the one we expect.

That was true before but only by convention, so f36af01 documents the invariant where the counters live and clamps the cached value at the update site: if an engine ever reported a reuse length longer than the prompt it was handed, the counter would now saturate instead of driving your prefill rate negative or inflating it by the cache share.

No slot/queue gauge added — requests_running / requests_waiting are the only gauges, as you suggested. /v1/models is unchanged, so owned_by is still dsgb10 on your side; nothing here assumes you stop the synthetic probe before you're ready.

Branch is at f36af01 on 100menotu001:frontend-metrics. Testing it against the four-Spark ring would be genuinely useful — in particular a cache-heavy agent lane where the cached share is large, since that is where a wrong subset relation would show up as an inflated prefill rate.

@robertung88

Copy link
Copy Markdown

Tested against the four-Spark ring, and it holds. Reporting the numbers because this is the case you asked about — a cache-heavy agent lane where the subset relation is load-bearing.

Setup: dsfront built from f36af01 (frontend only; engine binary and our local patches untouched), running as the production front end on the head via DSGB10_FRONT_BIN, serving DeepSeek-V4.1-Flash-1017 TP=4 on the switchless ring. Loaded cleanly, /health 200, /metrics 200 and open, no auth needed.

Consistency check against the engine's own request log. The front end writes one line per served request (session …: prompt N (cached M), completion K, …), so the counters can be reconciled exactly. After 5 requests on the agent lane:

from /metrics from the request log
prompt_tokens_total 477,029 477,029
prompt_tokens_cached_total 476,271 476,271
generation_tokens_total 2,225 (one request in flight) 1,156 completed

Exact match, and cached < prompt as documented. The derived figure our dashboard would compute — prompt − cached — is 758 tokens actually prefilled across 5 requests, which is right for a lane at 99.8 % prefix-cache reuse. That is the failure mode you were worried about not happening: nothing inflates the prefill rate by the cache share, and the clamp you added in f36af01 is not being exercised because the engine's cached_tokens is already bounded by the prompt it was handed.

requests_running / requests_waiting read 1 / 0 during an in-flight turn and 0 / 0 when idle, and the TTFT summary tracks (3 samples, 6.51 s sum — dominated by the first cold prefill after a restart, which is the expected shape).

Counters resetting on restart is visible and matches the PR description: they began at zero when the front end came up.

One thing you may want to know from the consumer side: the front end is also the place the session/prefix-cache state lives, so after a restart the counters and the cache both start clean — which means a scraper cannot mistake the reset for a traffic drop as long as the counter values only ever decrease to zero together. Worth a line in the docs if you document the endpoint.

We are keeping this build on the production front end for now (one env line, DSGB10_FRONT_BIN, reverts it). The dashboard itself still ignores /metrics for this backend — that is MiaAI-Lab/sparkDash#401 — but as soon as it scrapes, the series are there and correct. Thanks for turning the invariant into code rather than a convention.

…relation, and the restart reset

The endpoint shipped undocumented. A consumer testing f36af01 against a
four-Spark ring reconciled the counters against the request log (exact match,
cached < prompt) and asked for one thing from the docs: that a restart's reset
be stated, since the front end is also where the session and prefix-cache state
lives, so a scraper should not read the drop to zero as a traffic drop.

Lists the six series with their types, spells out that
prompt_tokens_cached_total is a subset of prompt_tokens_total (prefill is the
difference, and a partition would inflate it by the cache share), notes that
generation_tokens_total advances during decode while the prompt, cached and
TTFT series land at completion, and states that the counters reset to zero
together on a restart.
@100menotu001

Copy link
Copy Markdown
Author

Good — exact reconciliation against the request log is the check that matters, and 758 prefilled of 477,029 is the shape we wanted.

The reset is now documented rather than folklore: e54d885 adds "The /metrics endpoint" to docs/operations.md — the six series with their types, the subset relation spelled out (prompt − cached is prefill, and a partition would inflate it by the cache share), that generation_tokens_total advances during decode while the prompt/cached/TTFT series land at completion, and the line you asked for: a restart zeroes the counters and the session/prefix-cache state together, so a drop to zero is a restart and not a traffic drop.

Docs only; no code change, so the build you tested is unchanged apart from that file. SparkDash#401 is yours to close when you're ready to scrape.

@robertung88

Copy link
Copy Markdown

Docs commit noted — e54d885 is documentation only, so the build we are running (f36af01) is unchanged and stays in production on our ring. The restart wording is exactly what a scraper needs; thanks for taking the point.

On the dashboard side: MiaAI-Lab/sparkDash#401 is now a PR rather than an issue — MiaAI-Lab/sparkDash#411 wires the pico branch to read /metrics through the existing TensorFold reader, with the 404 path unchanged. It is running here against this build, so the counters you added are being consumed end to end: token totals and the ledger accrue, and the decode rate is labelled window rather than passed off as a smooth live figure. The one gap left on our side is prefill tok/s, which needs a prefill timing source in /health — your time_to_first_token_seconds_sum is the only candidate and TTFT is not the same quantity, so we have not faked it.

…ill_seconds

The front end already receives Done.prefill_us (prefill started -> first token)
and dropped it. sparkDash needs a prefill timing source to report prefill tok/s,
and TTFT is not one: it is measured from the front end and also covers the gate
and the submit round trip, so dividing by it understates the rate.

Add the summary over completed requests, so the rate is
d(prompt - cached) / d(request_prefill_seconds_sum) -- the computed tokens over
the time the engine spent computing them. Recorded where the prompt, cached and
TTFT counters are recorded, so one window's deltas cover the same requests.

The exposition moves into metrics_body() and a test checks every series is
present and every sample is a number.

Measured on the mock engine (--prefill-tps 2000, 426-token prompt): first
request 426 computed in 0.2156 s, second with 425 cached 1 computed, window
427 tokens / 0.2177 s = 1962 tok/s against the 2000 configured.
@100menotu001

Copy link
Copy Markdown
Author

The timing source was already on the wire and the front end was dropping it: Done.prefill_us (prefill started → first token, measured in engine/server/serve.cpp) arrives with every completion. d753a38 exports it as a summary over completed requests:

tensorfold:request_prefill_seconds_sum
tensorfold:request_prefill_seconds_count

It is on /metrics, not /health — /health stays the liveness body (ok / 503), and all the series live together on the unauthenticated /metrics you are already scraping. Your TensorFold reader already looks for request_prefill_seconds_* before falling back to request_prefill_time_seconds_*, so this needs no reader change: the rate is Δ(prompt_tokens_total − prompt_tokens_cached_total) / Δrequest_prefill_seconds_sum, the computed tokens over the time the engine spent computing them. TTFT appears nowhere in it.

Why TTFT could not stand in: it is measured from the front end from the moment the request is accepted, so it includes the gate wait and the submit round trip and is always at least prefill_us. On the mock engine here (426-token prompt, --prefill-tps 2000): TTFT 0.2158 s against prefill 0.2156 s for the cold request; over two requests, the second with 425 of 426 tokens cached, 427 computed tokens / 0.2177 s = 1962 tok/s against the 2000 configured. The subset relation stays load-bearing in that arithmetic — cached is subtracted from the prompt delta, never added as a second counter.

Recorded once per completed request, at the same site as the prompt/cached/TTFT counters, so one window's deltas cover the same requests; docs/operations.md documents the series and the derivation. Nothing else in the exposition changed and /v1/models is untouched, so owned_by is still dsgb10 on your side.

If you rebuild the front end from d753a38 on the ring, prefill tok/s should appear from your existing reader without touching #411 — and the cache-heavy agent lane is still the case worth checking, since a wrong subset relation there is what would show up as an inflated rate rather than a wrong-looking one.

@robertung88

Copy link
Copy Markdown

Built d753a38 onto the ring and the prefill rate is real — no reader change was needed, exactly as you said.

Cold, uncached prefill. A unique 20,481-token prompt (cached_tokens: 0), measured from the engine's own counters across the one finished request:

Δprompt_tokens_total           20,481
Δprompt_tokens_cached_total         0
Δrequest_prefill_seconds_sum   2.2594 s   (count +1)
=> 9,065 tok/s computed prefill

That lands on our independent baseline for this ring (8.1k–9.0k tok/s at 8k/32k context, measured with a separate harness before any of this existed), which is the cross-check I wanted: the new series is measuring the same thing the engine actually does, not a plausible-looking number.

The cache-heavy case you flagged. Same 20,481-token prompt sent again, fully cached this time (cached_tokens: 20,480):

Δprompt_tokens_total           20,481
Δprompt_tokens_cached_total    20,480
computed                            1
Δrequest_prefill_seconds_sum  0.02252 s
=> 44 tok/s computed

The subset relation holds (cached ≤ prompt), and the arithmetic degrades honestly: one computed token in 22 ms is a meaningless ratio, which is why our reader requires ≥50 ms and ≥50 tokens before it will publish an effective rate and otherwise falls back to a windowed split over finished requests. Nothing inflates — the failure mode you were guarding against would have shown up here as ~910k tok/s.

Consumer side, for completeness. With d753a38 on the front end the dashboard now reports uncachedPrefillTps 7,848 and cachedPrefillTps 66,676 (prefillTpsSource: completed-split) on the agent lane, alongside the token totals and request gauges. d753a38 is in production here via DSGB10_FRONT_BIN.

One small thing worth documenting if you touch docs/operations.md again: the first window after a restart divides by a prefill sum that starts at zero, so a scraper that keeps its own baseline across the restart will read one absurd rate before the series line up. We discard the first post-restart window; a line saying "baselines must be re-seeded after a restart, like the counters themselves" would save the next person the same discovery.

The restart paragraph said the counters and the session/prefix-cache state zero
together, which covers what a scraper reads but not what it remembers: a baseline
held across the restart still divides the new prefill sum by an old reference
point, so the first window after a restart reads one absurd rate before the
series line up again.

States the rule the consumer arrived at on their own (they discard the first
post-restart window) so the next person does not rediscover it. Docs only.
@100menotu001

Copy link
Copy Markdown
Author

Added in 94511c0, in the restart paragraph of ds41f-4xgx10-switchless/docs/operations.md: a scraper that keeps its own baseline across a restart has to re-seed it, because the first window divides by a prefill sum that begins at zero and a carried-over baseline reads one absurd rate before the series line up. It also names discarding the first post-restart window as the simple form of the rule, so the approach you arrived at is written down as the recommended one rather than a workaround.

Docs only — nothing on the wire changed, so d753a38 is still the build.

MiaAI-Lab pushed a commit to MiaAI-Lab/sparkDash that referenced this pull request Nov 16, 2026
pico-dgx-ds4's dsfront can serve GET /metrics with tensorfold:-prefixed
counters (Picom-code/pico-dgx-ds4#12): request gauges, generation/prompt/
cached token totals and a TTFT summary. That is the series set the
TensorFold reader already parses, so the pico branch now reads it instead
of always forcing generationTps = 0.

When the scrape yields a counter diff the decode rate is labelled
"window"; otherwise the dashboard-stream rate and the synthetic sample
keep working unchanged, so a build without /metrics is unaffected.

Counters are cleared before the scrape so a rate left by the previous
poll's sample is never mistaken for a counter-derived one.

(cherry picked from commit 8af80be)
vesatikkanen pushed a commit to vesatikkanen/pico-dgx-ds4 that referenced this pull request Nov 17, 2026
The endpoint, its series, the exposition's tests and the operations.md section
are 100menotu001's work, contributed as Picom-code#12. This branch
carried them over unchanged, but the commit that did so said only "ported from
Picom-code#12" in its body -- nothing in the tree said whose work it was, so a reader of
CHANGES.md, operations.md or server.rs would take it for this fork's.

Says so in all four places a reader looks: the CHANGES.md section header and its
opening paragraph, the operations.md section, the doc comments on the counters
and metrics_body(), and the deploy README's PR table and fork-work table.

No code changes: the front end's 63 tests still pass.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants